//
// Created by lzc on 2023/1/14.
//

#ifndef AOS_ARG_H
#define AOS_ARG_H

#include <getopt.h>
#include "stdlib.h"
#include "stdio.h"

typedef struct ServerArgs {
    int port;
    char *listen;
    int hidden;
    char *dir;
} ServerArgs;

typedef struct ClientArgs {
    int port;
    int list;
    char *server;
    char *dl;
    char *output;
} ClientArgs;

void parse_serv_args(ServerArgs *args, int argc, char *argv[]) {
    args->port = 123456;
    args->listen = "127.0.0.1";
    args->hidden = 0;
    args->dir = ".";

    struct option long_options[] = {
            {"listen", required_argument, NULL, 'l'},
            {"hidden", no_argument,       NULL, 'H'},
            {"port",   required_argument, NULL, 'p'},
    };

    int options_index = 0;
    int opt;
    while ((opt = getopt_long(argc, argv, "l:Hp:", long_options, &options_index)) != EOF) {
        switch (opt) {
            case 'l':
                args->listen = optarg;
                break;
            case 'H':
                args->hidden = 1;
                break;
            case 'p':
                args->port = strtol(optarg, NULL, 10);
                break;
            default:
                printf("unknown opt:%c\n", (char) opt);
        }
    }

    if (optind < argc){
        args->dir = argv[optind];
    }
}

void parse_cli_args(ClientArgs *args, int argc, char *argv[]) {
    args->port = 123456;
    args->list = 1;
    args->server = "127.0.0.1";

    struct option long_options[] = {
            {"list",   no_argument,       NULL, 'l'},
            {"server", required_argument, NULL, 's'},
            {"port",   required_argument, NULL, 'p'},
            {"dl",     required_argument, NULL, 'd'},
            {"output", required_argument, NULL, 'o'},
    };

    int options_index = 0;
    int opt;
    while ((opt = getopt_long(argc, argv, "ls:p:d:o:", long_options, &options_index)) != EOF) {
        switch (opt) {
            case 'l':
                break;
            case 's':
                args->server = optarg;
                break;
            case 'p':
                args->port = strtol(optarg, NULL, 10);
                break;
            case 'd':
                args->list = 0;
                args->dl = optarg;
                break;
            case 'o':
                args->output = optarg;
                break;
            default:
                printf("unknown opt:%c\n", (char) opt);
        }
    }

}

#endif //AOS_ARG_H
